data(us.cities)
# Get major cities for each sample region (state)
.states <- c("OR", "VT", "CO", "NC")
top.cities <- purrr::map_df(.states, function(s) {
out <- us.cities %>%
filter(country.etc==s) %>%
mutate(city = gsub(paste0(" ", s), "", name)) %>%
arrange(-pop)
if (s == "OR") {
out <- out %>%
head() %>%
filter(!(city %in% c("Gresham", "Hillsboro", "Corvallis",
"Beaverton", "Springfield")))
} else if (s == "CO") {
out <- out %>%
head() %>%
filter(!(city %in% c("Thornton", "Lakewood", "Aurora")))
} else if (s == "NC") {
out <- out %>%
head() %>%
filter(!(city %in% c("Greensboro", "Durham", "Fayetteville")))
} else {
out <- out %>% head()
}
out
})
# Load the map data
states <- map_data("state") %>%
filter(region %in% c("oregon", "north carolina", "colorado", "vermont"))
# Load your data
data.files <- list.files("../data/final", full.names = T)
df <- purrr::map_df(data.files, readRDS)
caps.after.ws <- function(string) {
gsub("(?<=\\s)([a-z])", "\\U\\1", string, perl = T)
}
# Define a function to create a plot for each species
plot.for.species <- function(spec, st.abbr) {
st <- case_when(st.abbr == "CO" ~ "colorado",
st.abbr == "NC" ~ "north carolina",
st.abbr == "VT" ~ "vermont",
st.abbr == "OR" ~ "oregon",
T ~ "")
title <- caps.after.ws(paste(st.abbr, gsub("_", " ", spec),
"Observations, 2016-2019"))
p <- ggplot(data = states %>% filter(region == st)) +
geom_polygon(aes(x = long, y = lat, group = group),
fill = "#989875", color = "black") +
geom_point(data = df %>% filter(state == st.abbr & common.name == spec),
aes(x = lon, y = lat),
size=1, alpha=.5, fill = "red", shape=21) +
geom_point(data = top.cities %>% filter(country.etc == st.abbr),
aes(x=long, y=lat),
fill="gold", color="black", size=3.5, shape = 21) +
geom_text(data = top.cities %>% filter(country.etc == st.abbr),
aes(x=long, y=lat, label=city),
color="white", hjust=case_when(st.abbr=="NC"~.2,
st.abbr=="VT"~.65,
T~.5),
vjust=ifelse(st.abbr=="NC", -.65, 1.5),
size=4) +
coord_map() +
ggtitle(title) +
theme_minimal() +
theme(panel.background = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank())
data.table(
state=st.abbr,
species=spec,
plot=list(p)
)
}
spec.state <- expand.grid(unique(df$common.name), unique(df$state)) %>%
rename(spec=Var1, st.abbr=Var2)
# Create a list of plots
plots <- purrr::map2_df(spec.state$spec,
spec.state$st.abbr,
~plot.for.species(.x, .y))
# Plot Ruddy Duck plots
do.call(ggpubr::ggarrange,
c(plots[species == "Ruddy Duck"]$plot,
list(nrow=2, ncol=2)))
# Plot Belted Kingfisher plots
do.call(ggpubr::ggarrange,
c(plots[species == "Belted Kingfisher"]$plot,
list(nrow=2, ncol=2)))
# Plot Wild Turkey plots
do.call(ggpubr::ggarrange,
c(plots[species == "Wild Turkey"]$plot,
list(nrow=2, ncol=2)))
# Plot Sharp-Shinned Hawk plots
do.call(ggpubr::ggarrange,
c(plots[species == "Sharp-shinned Hawk"]$plot,
list(nrow=2, ncol=2)))
# Plot Downy Woodpecker Plots
do.call(ggpubr::ggarrange,
c(plots[species == "Downy Woodpecker"]$plot,
list(nrow=2, ncol=2)))
# Plot Cedar Waxwing Plots
do.call(ggpubr::ggarrange,
c(plots[species == "Cedar Waxwing"]$plot,
list(nrow=2, ncol=2)))
# Plot Sandhill Crane Plots
do.call(ggpubr::ggarrange,
c(plots[species == "Sandhill Crane"]$plot,
list(nrow=2, ncol=2)))
# Plot Sanderling Plots
do.call(ggpubr::ggarrange,
c(plots[species == "Sanderling"]$plot,
list(nrow=2, ncol=2)))
states <- c("CO", "NC", "OR", "VT")
r.files <- paste0("../data/final_rasters/", states, ".tif")
r.list <- purrr::map(r.files, rast)
names(r.list) <- states
stratified.split.idx <- function(df, p=0.7, lat.lon.bins=25) {
# Cut along lat/lon values to create grids (lat.bin & lon.bin)
# lat.lon.bins is the number of divisions you want
df$lat.bin <- cut(df$lat, breaks=lat.lon.bins, labels = F)
df$lon.bin <- cut(df$lon, breaks=lat.lon.bins, labels = F)
# Create a new variable combining the stratification variables
df %>%
mutate(strata = paste(lat.bin, lon.bin, common.name, state)) %>%
pull(strata) %>%
# Create the data partitions
createDataPartition(., p = p, list = F) %>%
suppressWarnings()
}
prepare.data <- function(df, p=.7, lat.lon.bins=25) {
train.index <- stratified.split.idx(df, p=p, lat.lon.bins = lat.lon.bins)
df.train <- df[train.index, ]
df.test <- df[-train.index, ]
list(train = df.train,
test = df.test,
index = train.index)
}
train.test <- prepare.data(df, .7)
train <- df[train.test$index,]
test <- df[-train.test$index,]
Each of the 20 different Land Cover Categories falls under a “parent” category (see National Land Cover Database Class Legend and Description).